using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Html;
using System.IO;
using System.Xml;
using System;
using System.Text;

namespace UnitTest {
    /// <summary>
    /// 
    /// </summary>
    [TestClass]
    public class HtmlParseTest {
        public HtmlParseTest() {
        }

        //[TestMethod]
        public void ParseTest() {

            HtmlParser _parser = new HtmlParser();
            XmlDocument _doc = _parser.Parse(Properties.Resources.HtmlSample);
            XmlWriterSettings _settings = new XmlWriterSettings();
            _settings.Indent = true;
            XmlWriter _writer = XmlWriter.Create("parsetest.google.xml", _settings);
            _doc.Save(_writer);

            HtmlFormatter _formater = new HtmlFormatter();

            StringBuilder _sb = new StringBuilder();
            foreach (XmlNode _node in _doc.DocumentElement.ChildNodes) {
                _formater.Format(_node, _sb);
            }
            File.WriteAllText("parsetest.google.htm", _sb.ToString());
        }


        //[TestMethod]
        public void EndToEndTest() {
            HtmlDocument _document = new HtmlDocument();
            _document.LoadHTML(Properties.Resources.HtmlSample);
            _document.Save("endtoendtest.google.xml");
            File.WriteAllText("endtoendtest.google.htm", _document.GetHtml());
        }
        
        [TestMethod]
        public void TagTest() {
            HtmlDocument _document = new HtmlDocument();
            _document.LoadHTML(Properties.Resources.TagTest);
            //string _htmlData = _document.Load(new Uri("http://www.google.com"));
            //_document.LoadHTML(_htmlData);
            _document.Save("TagTest.xml");
            File.WriteAllText("TAGTEST.HTM", _document.GetHtml());
            //File.WriteAllText("RAW.HTM", _htmlData);
        }

        //[TestMethod]
        public void CDATAEscapeTest() {
            HtmlDocument _document = new HtmlDocument();
            _document.LoadHTML(Properties.Resources.Mininova);
            string _htmlData = _document.OuterXml;
            _document.Save("CDATAEscapeTest.xml");
            File.WriteAllText("CDATAEscapeTest.HTM", _document.GetHtml());
        }

        //[TestMethod]
        public void UnicodeTest() {
            HtmlDocument _document = new HtmlDocument();
            ///you need an internet connection for this
            _document.Load(new Uri("http://www.google.co.jp/search?hl=ja&q=the&lr=&aq=f&oq="));
            _document.Save("UnicodeTest.google_jp.xml");
            //save that data
            File.WriteAllText("UnicodeTest.google_jp.htm", _document.GetHtml());
        }

        //[TestMethod]
        public void XSLTTransformTest() {
            HtmlDocument _document = new HtmlDocument();
            _document.LoadHTML(Properties.Resources.HtmlSample);

            HtmlDocument _transformed = _document.Transform(Properties.Resources.GoogleTransform);

            _transformed.Save("XSLTTransformTest.google.xml");
        }

        //[TestMethod]
        public void XSLTCacheTest() {
            string _sample = Properties.Resources.HtmlSample;
            string _template = Properties.Resources.GoogleTransform;

            HtmlDocument _document = new HtmlDocument();
            DateTime _start = DateTime.Now; //start time
            _document.LoadHTML(_sample);
            HtmlDocument _transformed = _document.Transform(_template);
            DateTime _stop = DateTime.Now;
            TimeSpan _initialSpeed = _stop - _start;

            _start = DateTime.Now;
            //execute again
            _document.LoadHTML(_sample);
            _transformed = _document.Transform(_template);
            _stop = DateTime.Now;

            TimeSpan _finalSpeed = _stop - _start;

            Assert.IsTrue(_initialSpeed > _finalSpeed, "There was no execution speed improvement in the XSLT transform");

            _document = new HtmlDocument();

            _start = DateTime.Now;
            //one more time to confirm that it was the cache improving multiple transform speed
            _document.LoadHTML(_sample);
            _transformed = _document.Transform(_template);
            _stop = DateTime.Now;

            _finalSpeed = _stop - _start;
            //trimm the miliseconds
            Assert.IsTrue(_initialSpeed.Seconds  == _finalSpeed.Seconds , "Clearing of the cache did not result in the expected execution speed");

        }

        //[TestMethod]
        public void RandomSiteTest() {
            HTTPSiteWrapper _wrapper = new HTTPSiteWrapper(new Uri("http://www.classiccarsglobal.com"));
            string _page = _wrapper.GetPage(new Uri("http://www.classiccarsglobal.com/dealers.asp"));

            HtmlDocument _document = new HtmlDocument();
            _document.LoadHTML(_page);
            _document.Save("RandomSiteTest.xml");
            File.WriteAllText("RandomSiteTest.htm", _document.GetHtml());
        }
    }
}
